package de.fuberlin.inf.alp2;

import de.fuberlin.inf.alp2.utils.ConsoleInput;

public class TestConsoleApplication {

	public static void main(String[] args) {
		int userInput = 1;
		while (true)
		{
			System.out.println("Which exercise do you want to check now?\n");
			System.out.println("    1. Methods manhattan() and manhatten2() " +
					"in Class Point");
			System.out.println("    2. RingBuffer");
			System.out.println("    3. Methods reverseIt() and reverseRek() " +
					"in Class CLList");
			System.out.println("    4. Josephus-Ring");
			System.out.println("\n    0. Exit application");
			userInput = ConsoleInput.readInt("Your choice?", "Try again!");
			switch (userInput) {
			case 0:
				System.out.println("Bye...");
				System.exit(0);
			case 1:
				int x,y;
				System.out.println("Where to place 1st point?");
				x = ConsoleInput.readInt("Abscissa (x-coordinate)?", "Sure?");
				y = ConsoleInput.readInt("Ordinate (y-coordinate)?", "Sure?");
				Point p1 = new Point(x,y);
				System.out.println("Where to place 2nd point?");
				x = ConsoleInput.readInt("Abscissa (x-coordinate)?", "Sure?");
				y = ConsoleInput.readInt("Ordinate (y-coordinate)?", "Sure?");
				Point p2 = new Point(x,y);
				System.out.println("The manhattan distance between " + p1 +
						" and " + p2 + " is " + p1.manhattan(p2));
				System.out.println("The manhattan distance between " + p1 +
						" and last created Point is " + p1.manhattan2());
				
				break;
			case 2:
				int size = 0;
				size = ConsoleInput.readInt("Specify size of ringbuffer",
						"Try again!");
				RingBuffer rb = new RingBuffer(size);
				int count = 0;
				while (userInput != 0){
					System.out.println("    1. Add new object");
					System.out.println("    2. Remove an object");
					System.out.println("\n    0. Back to main menu");
					userInput = ConsoleInput.readInt("Your choice?",
							"Try again!");
					switch (userInput){
					case 0:
						break;
					case 1:
						try {
							rb.enqueue(++count);
							System.out.println("The object " + count + " has " +
									"been sucessfully enqueued.");
						}
						catch (Exception e){
							System.out.println(e.getMessage());
						}
						break;
					case 2:
						try {
							Object obj = rb.dequeue();
							System.out.println("The object " + obj + " has " +
									"been sucessfully dequeued.");
						}
						catch (Exception e){
							System.out.println(e.getMessage());
						}
						break;
					}
				}
				break;
			case 3:
				CLList list = new CLList();
				char chr = 'A';
				while (userInput != 0){
					System.out.println("    1. Add new object (at beginning)");
					System.out.println("    2. ReverseIt()");
					System.out.println("    3. ReverseRek()");
					System.out.println("    4. Show list");
					System.out.println("\n    0. Back to main menu");

					userInput = ConsoleInput.readInt("Your choice?",
							"Try again!");
					switch (userInput){
					case 0:
						break;
					case 1:
						list.addFirst(chr);
						System.out.println("The object " + chr++ + " has " +
								"been sucessfully enqueued.");
						break;
					case 2:
						list.reverseIt();
						break;
					case 3:
						list.reverseRek();
						break;
					case 4:
						System.out.println(list);
					}
				}
				break;
			case 4:
				count = 0;
				size = 0;
				CLList prisoners = new CLList();
				size = ConsoleInput.readInt("How many prisoners?", "sure?");
				while (count < size)
					prisoners.addFirst(new Prisoner("G" + count++));
				prisoners.reverseRek();
				
				int fatal = ConsoleInput.readInt("Fatal number?", "sure?");
				System.out.println("Executed:");
				Node current = prisoners.getNode(1);
				while (prisoners.size() > 1) {
					for (int i=1; i < fatal; i++)
						current = prisoners.getNextRing(current);
					prisoners.remove(current);
					System.out.println("\t" + current.getValue());
					current = prisoners.getNextRing(current);
				}
				System.out.println("Reprieved:");
				System.out.println("\t" + current.getValue());
				break;
			default:
				System.out.println("Please choose from available options");
			}
		}
	}
}
